local skel = fk.CreateSkill{
  name = "ofl__jingce",
}
---@type TrigSkelSpec<EventTurnChangingFunc>
local jingce = {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      local room = player.room
      local discardnum = 0
      local usenum = 0
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.toArea == Card.DiscardPile then
            for _, info in ipairs(move.moveInfo) do
              discardnum = discardnum + 1
            end
          end
        end
        return false
      end, turn_event.id)
      room.logic:getEventsOfScope(GameEvent.UseCard, 998, function(e) 
        local use = e.data[1]
        if use.from == player.id then
          usenum = usenum + 1
        end
      end, Player.HistoryTurn)
      if discardnum >= player.hp or usenum >= player.hp then
        self.cost_data = {
          discardnum = discardnum,
          usenum = usenum,
        }
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if self.cost_data.discardnum >= player.hp and room:askForSkillInvoke(player, self.name, nil, "#ofl__jingce-draw") then
      self.cost_data.drawaccept = true
      return true
    end
    if self.cost_data.usenum >= player.hp and room:askForSkillInvoke(player, self.name, nil, "#ofl__jingce-play") then
      self.cost_data.playaccept = true
      return true
    end
    return
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if self.cost_data.drawaccept then
      player:gainAnExtraPhase(Player.Draw)
      if not player.dead and room:askForSkillInvoke(player, self.name, nil, "#ofl__jingce-play") then
        player:gainAnExtraPhase(Player.Play)
      end
    end
    if not player.dead and self.cost_data.playaccept then
      self.cost_data.playaccept = false
      player:gainAnExtraPhase(Player.Play)
    end
  end,
}
skel:addEffect(fk.TurnEnd, jingce)
return skel